package cn.wikiflyer.beikang.act.home;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import cn.wikiflyer.beikang.BaseActivity;
import cn.wikiflyer.beikang.R;
import cn.wikiflyer.beikang.act.home.adapter.ADAdapter;
import cn.wikiflyer.beikang.act.user.bean.ShareBean;
import cn.wikiflyer.beikang.bean.HomeBean;
import cn.wikiflyer.beikang.bean.HomeBean.ActivityBean;
import cn.wikiflyer.beikang.bean.HomeBean.BannerBean;
import cn.wikiflyer.beikang.bean.HomeBean.HomeNewsBean;
import cn.wikiflyer.beikang.bean.base.BKBaseBean;
import cn.wk.libs4a.net.WKAsyncTaskPro;
import cn.wk.libs4a.view.adview.CircleFlowIndicator;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshBase;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import cn.wk.libs4a.view.pulltorefresh.PullToRefreshListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * Tab Home页
 */
public class HomeAct extends BaseActivity {

	private PullToRefreshListView mPullToListView;
	private ListView mListView;
	private HomeListAdapter mHomeAdapter;

	private View list_header;

	private View list_footer;
	private View home_more;
	// private ADViewUtils adUtils;
	// 顶部Viewpager
	private ViewPager viewpager;
	private CircleFlowIndicator flowIndicator;
	private ADAdapter mHomeGalleryAdapter;

	private View allnews;// 查看全部

	// 自动滚动
	private long time = 5 * 1000;
	private Handler mPagerHandler;
	private Runnable mAutoPlayTask = new Runnable() {
		public void run() {
			if (viewpager.getCurrentItem() < mHomeGalleryAdapter.getCount() - 1) {
				viewpager.setCurrentItem(viewpager.getCurrentItem() + 1);
			} else {
				viewpager.setCurrentItem(0);
			}
			mPagerHandler.postDelayed(mAutoPlayTask, time);
		}
	};

	@Override
	public void loadXml() {
		setContentView(R.layout.bk_act_home);
		// 头
		list_header = View.inflate(this, R.layout.bk_act_home_header, null);
		viewpager = (ViewPager) list_header.findViewById(R.id.gallery);
		flowIndicator = (CircleFlowIndicator) list_header
				.findViewById(R.id.circleFlowIndicator);
		mHomeGalleryAdapter = new ADAdapter(ctx);
		viewpager.setAdapter(mHomeGalleryAdapter);
		mPagerHandler = new Handler();
		mPagerHandler.postDelayed(mAutoPlayTask, time);
		allnews = list_header.findViewById(R.id.allnews);
		allnews.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(HomeAct.this, NewsListAct.class));
			}
		});
		// 尾
		list_footer = View.inflate(this, R.layout.bk_act_home_footer, null);
		home_more = list_footer.findViewById(R.id.more);
		home_more.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				startActivity(new Intent(HomeAct.this, NewsListAct.class));
			}
		});
	}

	@Override
	public void init() {
		// WKCheckAppUpdate.getInstance().check(this);
		// WKCheckAppUpdate.getInstance().check(this, new GetDataForUpdate() {
		// public String getData() {
		// return app().net.get_version();
		// }
		// });
		initList();
	}

	private void initList() {
		mPullToListView = (PullToRefreshListView) findViewById(R.id.listview1);
		mListView = mPullToListView.getRefreshableView();
		mHomeAdapter = new HomeListAdapter(ctx);

		mListView.addHeaderView(list_header);
		mListView.addFooterView(list_footer);
		mListView.setAdapter(mHomeAdapter);
		mPullToListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				new GetHomeTask(ctx);
				new GetShare(ctx, "1");
				new GetShare(ctx, "2");
			}
		});
	}

	@Override
	public void setData() {
		new GetHomeTask(ctx);
		new GetShare(ctx, "1");
		new GetShare(ctx, "2");
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	/**********************************************************************
	 * 数据接口
	 ***********************************************************************/
	/**
	 * 获取首页数据
	 */
	class GetHomeTask extends WKAsyncTaskPro {
		public GetHomeTask(Context context) {
			super(context, true, false);
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getIndex(isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			mPullToListView.onRefreshComplete();
			Type cvbType = new TypeToken<BKBaseBean<HomeBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<HomeBean> base = gson.fromJson(result, cvbType);
			if (base.status == 1) {
				setAdData(base.data.ad);
				setNewsData(base.data.news);
				setActData(base.data.activity);
			} else {
			}
		}
	}

	private void setAdData(ArrayList<BannerBean> data) {
		mHomeGalleryAdapter.setData(data);
		flowIndicator.setGallery(mHomeGalleryAdapter.getCount());
		viewpager.setCurrentItem(0);
		viewpager.setOnPageChangeListener(new CommentPageChangeListener());
	}

	/**
	 * 设置咨询数据
	 */
	private void setNewsData(ArrayList<HomeNewsBean> mNewsData) {
		mHomeAdapter.setData(mNewsData);
	}

	/**
	 * 设置活动数据
	 */
	private void setActData(ArrayList<ActivityBean> mActBeans) {
		new HomeBottom(ctx, list_footer, mActBeans);
	}

	/**
	 * 页面滑动触发事件
	 */
	class CommentPageChangeListener implements OnPageChangeListener {
		public void onPageScrollStateChanged(int arg0) {
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageSelected(int position) {
			flowIndicator.setPosition(position);
		}
	}

	/**
	 * 获取首页数据 type 请求类型 1推荐 2分享
	 */
	class GetShare extends WKAsyncTaskPro {
		String type = "";

		public GetShare(Context context, String type) {
			super(context, true);
			this.type = type;
		}

		@Override
		protected String doInBackground(boolean... isLocal) {
			return app().net.getShare(isLocal);
		}

		@Override
		protected void doOnPostExecute(String result, boolean isLocal) {
			Type cvbType = new TypeToken<BKBaseBean<ShareBean>>() {
			}.getType();
			Gson gson = new Gson();
			BKBaseBean<ShareBean> base = gson.fromJson(result, cvbType);
			System.out.println("" + result);
			if (base.status == 1) {
				if (type.equals("1")) {// 推荐
					app().shareTuijian.share_text = base.data.share_text;
					app().shareTuijian.share_url = base.data.share_url;
				} else if (type.equals("2")) {// 分享
					app().shareFenXiang.share_text = base.data.share_text;
					app().shareFenXiang.share_url = base.data.share_url;
				}
			} else {
			}
		}
	}

}